<?php
namespace app\common\model;
use think\Model;
use think\Log;

class DeviceMessage extends Model {
	protected $tableName = 'device_message';

	public function addmessage($device_id,$data){
		// dump($data);
		$add['device_id'] = $device_id;
		$add['date'] = substr($data[1],4).'-'.substr($data[1],2,2).'-'.substr($data[1],0,2);
		$add['time'] = $data[2];
		$add['latitude'] = $data[4];
		$add['longitude'] = $data[6];
		$add['speed'] = $data[8];
		$add['direction'] = $data[9];
		$add['altitude'] = $data[10];
		$add['satellite_number'] = $data[11];
		$add['gsm'] = $data[12];
		$add['electric'] = $data[13];
		$add['step_number'] = $data[14];
		$add['turn_number'] = $data[15];
		$add['device_status'] = $data[16];
		$add['base_station'] = $data[17];
		$base_info = '';
		$num = count($data);
		$bts = '';
		$n = 0;//WiFi定位的数组数
		$wifi ='|';
		for ($i=19; $i < count($data); $i++) { 
			if($i < 24){//基站定位
				$bts .= ','.$data[$i];
			}
			if(substr($data[$i], 0,1).$n == 'W'.$n){//截取一个字符串的时候等于W0，W1等为WiFi地址
				$wifi .= $data[$i+1].','.$data[$i+2].','.'TP-link|';
			}
			$base_info .= ','.$data[$i];
		}
		$wifi = substr($wifi,1);
		$bts = substr($bts,1);
		$add['base_info'] = substr($base_info,1);

		$add['wifi_num'] = $data[$num-1];
		if($add['longitude'] == 0 || $add['latitude'] == 0){
			if($wifi){//调用基站定位

				$url = "http://apilocate.amap.com/position?accesstype=1&cdma=0&macs={$wifi}&output=json&key=abc6a8805c39bd1713baccf90132855a";
				//http://apilocate.amap.com/position?accesstype=0&imei=352315052834187&cdma=0&bts=460,0,9341,4082,-91&output=json&key=abc6a8805c39bd1713baccf90132855a
				Log::record($url,'error');
				$result = file_get_contents($url);
				Log::record($result,'error');
				$result = json_decode($result,true);
				$gps = explode(',',$result["result"]["location"]);
				if(isset($gps[1])){
					$add['longitude'] = $gps[0];
					$add['latitude'] = $gps[1];
					$add['type'] = '2';
				}
				
			}else if($bts){//调用基站定位

				$url = "http://apilocate.amap.com/position?accesstype=0&cdma=0&bts={$bts}&output=json&key=abc6a8805c39bd1713baccf90132855a";
				/*http://apilocate.amap.com/position?accesstype=0&imei=352315052834187&cdma=0&bts=460,0,9341,4082,-91&output=json&key=abc6a8805c39bd1713baccf90132855a*/
				Log::record($url,'error');
				$result = file_get_contents($url);
				Log::record($result,'error');
				$result = json_decode($result,true);
				$gps = explode(',',$result["result"]["location"]);
				if(isset($gps[1])){
					$add['longitude'] = $gps[0];
					$add['latitude'] = $gps[1];
					$add['type'] = '3';
				}
				
			}
		}else{
			//Log::record('changeonegpsnew','error');
			$add = changeonegpsnew($add);
			$add['type'] = '1';
		}
		

		$where = array('device_id'=>$add['device_id']);
		$data = array('longitude'=>$add['longitude'],'latitude'=>$add['latitude'],'type'=>$add['type'],'update_time'=>date('Y-m-d H:i:s'));
		// dump($data);
		if($data['longitude']){
			Model('DeviceStatus')->savestatus($where,$data);
		}
		
		// dump($add);
		$bool = Model($this->tableName)->save($add);
		// dump($bool);
		return $bool;
	}
}